**************************************************************
*Figure 5a Predictions
**************************************************************

use "PartySupport_MASTER.dta"

set more off 

set seed 1234

gen AbsNominate=abs(nominate_dim1)
gen VoteShare_lag=dv_lag if party_code==100
replace VoteShare_lag=100-dv_lag if party_code==200
gen presvote=dpres if party_code==100
replace presvote=100-dpres if party_code==200

egen distgroup=group(state_abbrev district_code)

*VOTETYPES
*Regular Passage Category
gen RegPass=1 if VoteType==11 | VoteType==12 | VoteType==14 | VoteType==30
replace RegPass=0 if RegPass==.
label var RegPass "Regular Passage Category, VoteType types 11, 12, 14, 30"
*Suspensions Category
gen Susp=1 if VoteType==15 | VoteType==16 | VoteType==33 | VoteType==68
replace Susp=0 if Susp==.
label var Susp "Suspension Category, VoteType types 15, 16, 33, 68"
*Other Passage Category
gen OtherPass= 1 if VoteType==1 | VoteType==13 | VoteType==17 | VoteType==18 | VoteType==19 | VoteType==31 | VoteType==32
replace OtherPass=0 if OtherPass==.
label var OtherPass "Other Passage Category, VoteType types 1, 13, 17, 18, 19, 31, 32"
*Amendments Category
gen Amend=1 if VoteType==21 | VoteType==22 | VoteType==23 | VoteType==24 | VoteType==25 | VoteType==26 | VoteType==27 | VoteType==28 | VoteType==29 | VoteType==80
replace Amend=0 if Amend==.
label var Amend "Amendments Category, VoteType types 21, 22, 23, 24, 25, 26, 27, 28, 29, 80"
*Procedural Partisan Category
gen ProPart=1 if VoteType==61 | VoteType==76 | VoteType==77 | VoteType==79 | VoteType==81 | VoteType==82 | VoteType==88 | VoteType==89 | VoteType==93 | VoteType==95 | VoteType==97 | VoteType==99
replace ProPart=0 if ProPart==.
label var ProPart "Procedural Partisan Category, VoteType types 61, 76, 77, 79, 81, 82, 88, 89, 93, 95, 97, 99"
*Miscellaneous Procedural Category
gen MiscPro=1 if RegPass==0 & Susp==0 & OtherPass==0 & Amend==0 & ProPart==0
replace MiscPro=0 if MiscPro==.
label var MiscPro "Miscellaneous Procedural Category, VoteType types: All others"

replace Cycle=Cycle-1

gen DaysToElecPres=DaysToElection*presvote
gen DaysToElecCycle=DaysToElection*Cycle
gen PresCycle=presvote*Cycle
gen DaysToElecPresCycle=DaysToElection*presvote*Cycle
gen DaysToElec2Pres=DaysToElection2*presvote
gen DaysToElec2Cycle=DaysToElection2*Cycle
gen DaysToElec2PresCycle=DaysToElection2*presvote*Cycle
gen DaysToElec3Pres=DaysToElection3*presvote
gen DaysToElec3Cycle=DaysToElection3*Cycle
gen DaysToElec3PresCycle=DaysToElection3*presvote*Cycle

logit PartyUnityVote DaysToElection presvote Cycle DaysToElecPres DaysToElecCycle DaysToElection2 DaysToElec2Pres DaysToElection3 DaysToElec3Pres QualChall QualChall_lag Freshman InpartyMidterm PresApproval_In rdi_q3_In PartyOfPres PrimaryChall redist seniorit Retirement_Jacob AbsNominate VoteShare_lag RegPass Susp MiscPro Amend ProPart if PresPartyUnity==1, vce(cluster distgroup)

drawnorm M1_b1-M1_b28, n(10000) means(e(b)) cov(e(V)) clear

*Enter values of variables

*EARLY PERIOD (FOR ALL VARIABLES)
forvalues i=1(1)735 {
scalar days=`i'
scalar days2=days^2
scalar days3=days^3
scalar presvote=53.80947
scalar cycle=0
scalar dayspres=days*presvote
scalar dayscycle=days*cycle
scalar prescycle=presvote*cycle
scalar daysprescycle=days*presvote*cycle
scalar days2pres=days2*presvote
scalar days3pres=days3*presvote
scalar QualChall=.2365175
scalar QualChall_lag=.2285399
scalar Freshman=.2662885
scalar InpartyMidterm=.285537 
scalar PresApproval_In=-.803758
scalar rdi_q3_In=.1404514
scalar PartyOfPres=.4888875
scalar PrimaryChall=.3183035
scalar redist=.3576258 
scalar seniorit=6.469113
scalar Retirement_Jacob=.0899082
scalar AbsNominate=.2919597
scalar VoteShare_lag=66.27492
scalar RegPass=.4328308
scalar Susp=.0742217
scalar MiscPro=.0612949
scalar Amend=.187322 
scalar ProPart=.1963771  
scalar constant=1
generate x_betahatA_`i'=M1_b1*days + M1_b2*presvote + M1_b3*cycle + M1_b4*dayspres + M1_b5*dayscycle + M1_b6*days2 + M1_b7*days2pres + M1_b8*days3 + M1_b9*days3pres + M1_b10*QualChall + M1_b11*QualChall_lag + M1_b12*Freshman + M1_b13*InpartyMidterm + M1_b14*PresApproval_In + M1_b15*rdi_q3_In + M1_b16*PartyOfPres + M1_b17*PrimaryChall + M1_b18*redist + M1_b19*seniorit + M1_b20*Retirement_Jacob + M1_b21*AbsNominate + M1_b22*VoteShare_lag + M1_b23*RegPass + M1_b24*Susp + M1_b25*MiscPro + M1_b26*Amend + M1_b27*ProPart + M1_b28*constant
generate predA_`i'=1/(1+exp(-(x_betahatA_`i')))
drop x_betahatA_`i'
}

*EARLY FOR CYCLE & VOTES (BUT LATE FOR PRESVOTE - TO REPRESENT CHANGE IN VULNERABILITY)
forvalues i=1(1)735 {
scalar days=`i'
scalar days2=days^2
scalar days3=days^3
scalar presvote=63.64906
scalar cycle=0
scalar dayspres=days*presvote
scalar dayscycle=days*cycle
scalar prescycle=presvote*cycle
scalar daysprescycle=days*presvote*cycle
scalar days2pres=days2*presvote
scalar days3pres=days3*presvote
scalar QualChall=.2365175
scalar QualChall_lag=.2285399
scalar Freshman=.2662885
scalar InpartyMidterm=.285537 
scalar PresApproval_In=-.803758
scalar rdi_q3_In=.1404514
scalar PartyOfPres=.4888875
scalar PrimaryChall=.3183035
scalar redist=.3576258 
scalar seniorit=6.469113
scalar Retirement_Jacob=.0899082
scalar AbsNominate=.2919597
scalar VoteShare_lag=66.27492
scalar RegPass=.4328308
scalar Susp=.0742217
scalar MiscPro=.0612949
scalar Amend=.187322 
scalar ProPart=.1963771  
scalar constant=1
generate x_betahatB_`i'=M1_b1*days + M1_b2*presvote + M1_b3*cycle + M1_b4*dayspres + M1_b5*dayscycle + M1_b6*days2 + M1_b7*days2pres + M1_b8*days3 + M1_b9*days3pres + M1_b10*QualChall + M1_b11*QualChall_lag + M1_b12*Freshman + M1_b13*InpartyMidterm + M1_b14*PresApproval_In + M1_b15*rdi_q3_In + M1_b16*PartyOfPres + M1_b17*PrimaryChall + M1_b18*redist + M1_b19*seniorit + M1_b20*Retirement_Jacob + M1_b21*AbsNominate + M1_b22*VoteShare_lag + M1_b23*RegPass + M1_b24*Susp + M1_b25*MiscPro + M1_b26*Amend + M1_b27*ProPart + M1_b28*constant
generate predB_`i'=1/(1+exp(-(x_betahatB_`i')))
drop x_betahatB_`i'
}

*LATE FOR ALL
forvalues i=1(1)735 {
scalar days=`i'
scalar days2=days^2
scalar days3=days^3
scalar presvote=63.64906
scalar cycle=31
scalar dayspres=days*presvote
scalar dayscycle=days*cycle
scalar prescycle=presvote*cycle
scalar daysprescycle=days*presvote*cycle
scalar days2pres=days2*presvote
scalar days3pres=days3*presvote
scalar QualChall=.098179
scalar QualChall_lag=.1419985
scalar Freshman=.2537902
scalar InpartyMidterm=.5281861
scalar PresApproval_In=-.994198
scalar rdi_q3_In=.3418647 
scalar PartyOfPres=.5460493
scalar PrimaryChall=.3716194
scalar redist=.0425769 
scalar seniorit=5.573624
scalar Retirement_Jacob=.1415856
scalar AbsNominate=.4427426
scalar VoteShare_lag=63.71035
scalar RegPass=.0564774
scalar Susp=.05562
scalar MiscPro=.7091023
scalar Amend=.0633329 
scalar ProPart=.1068932 
scalar constant=1
generate x_betahatC_`i'=M1_b1*days + M1_b2*presvote + M1_b3*cycle + M1_b4*dayspres + M1_b5*dayscycle + M1_b6*days2 + M1_b7*days2pres + M1_b8*days3 + M1_b9*days3pres + M1_b10*QualChall + M1_b11*QualChall_lag + M1_b12*Freshman + M1_b13*InpartyMidterm + M1_b14*PresApproval_In + M1_b15*rdi_q3_In + M1_b16*PartyOfPres + M1_b17*PrimaryChall + M1_b18*redist + M1_b19*seniorit + M1_b20*Retirement_Jacob + M1_b21*AbsNominate + M1_b22*VoteShare_lag + M1_b23*RegPass + M1_b24*Susp + M1_b25*MiscPro + M1_b26*Amend + M1_b27*ProPart + M1_b28*constant
generate predC_`i'=1/(1+exp(-(x_betahatC_`i')))
drop x_betahatC_`i'
}

foreach var of varlist predA_1-predA_735{
egen `var'a=clsort(`var')
}

drop predA_1-predA_735

foreach var of varlist predB_1-predB_735{
egen `var'b=clsort(`var')
}

drop predB_1-predB_735

foreach var of varlist predC_1-predC_735{
egen `var'c=clsort(`var')
}

drop predC_1-predC_735

save "Figure5a_Values.dta"

drop M1_b1-M1_b28
gen days=_n
*Keep the 8.25th and 91.75th percentiles
keep if days==825 | days==9175
xpose, clear
rename v1 LowBound
rename v2 UpBound
drop in 2206
gen days=_n
move days LowBound
gen Type="A - All Early" if days<736
replace Type="B - Late Presvote" if days>735 & days<1471
replace Type="C - All Late" if days>1470 & days<2206
replace days=days-735 if Type=="B - Late Presvote"
replace days=days-1470 if Type=="C - All Late"

*sort days

save "Figure5a_Preds.dta"

clear
*Reshape for figure
use "Figure5a_Preds.dta"
gen shaper=1 if Type=="A - All Early"
replace shaper=2 if Type=="B - Late Presvote"
replace shaper=3 if Type=="C - All Late"
drop Type
reshape wide LowBound UpBound, i(days) j(shaper)
save "Figure5a_Preds_reshaped.dta"
clear



**************************************************************
*Figure 5b Predictions
**************************************************************

use "PartySupport_MASTER.dta"

set more off 

set seed 1234

gen AbsNominate=abs(nominate_dim1)
gen VoteShare_lag=dv_lag if party_code==100
replace VoteShare_lag=100-dv_lag if party_code==200
gen presvote=dpres if party_code==100
replace presvote=100-dpres if party_code==200

egen distgroup=group(state_abbrev district_code)

*VOTETYPES
*Regular Passage Category
gen RegPass=1 if VoteType==11 | VoteType==12 | VoteType==14 | VoteType==30
replace RegPass=0 if RegPass==.
label var RegPass "Regular Passage Category, VoteType types 11, 12, 14, 30"
*Suspensions Category
gen Susp=1 if VoteType==15 | VoteType==16 | VoteType==33 | VoteType==68
replace Susp=0 if Susp==.
label var Susp "Suspension Category, VoteType types 15, 16, 33, 68"
*Other Passage Category
gen OtherPass= 1 if VoteType==1 | VoteType==13 | VoteType==17 | VoteType==18 | VoteType==19 | VoteType==31 | VoteType==32
replace OtherPass=0 if OtherPass==.
label var OtherPass "Other Passage Category, VoteType types 1, 13, 17, 18, 19, 31, 32"
*Amendments Category
gen Amend=1 if VoteType==21 | VoteType==22 | VoteType==23 | VoteType==24 | VoteType==25 | VoteType==26 | VoteType==27 | VoteType==28 | VoteType==29 | VoteType==80
replace Amend=0 if Amend==.
label var Amend "Amendments Category, VoteType types 21, 22, 23, 24, 25, 26, 27, 28, 29, 80"
*Procedural Partisan Category
gen ProPart=1 if VoteType==61 | VoteType==76 | VoteType==77 | VoteType==79 | VoteType==81 | VoteType==82 | VoteType==88 | VoteType==89 | VoteType==93 | VoteType==95 | VoteType==97 | VoteType==99
replace ProPart=0 if ProPart==.
label var ProPart "Procedural Partisan Category, VoteType types 61, 76, 77, 79, 81, 82, 88, 89, 93, 95, 97, 99"
*Miscellaneous Procedural Category
gen MiscPro=1 if RegPass==0 & Susp==0 & OtherPass==0 & Amend==0 & ProPart==0
replace MiscPro=0 if MiscPro==.
label var MiscPro "Miscellaneous Procedural Category, VoteType types: All others"

gen post1994elec=1 if Cycle>=21
replace post1994elec=0 if post1994elec==.

gen DaysToElecPres=DaysToElection*presvote
gen DaysToElecPost1994=DaysToElection*post1994elec
gen PresPost1994=presvote*post1994elec
gen DaysToElecPresPost1994=DaysToElection*presvote*post1994elec
gen DaysToElec2Pres=DaysToElection2*presvote
gen DaysToElec2Post1994=DaysToElection2*post1994elec
gen DaysToElec2PresPost1994=DaysToElection2*presvote*post1994elec
gen DaysToElec3Pres=DaysToElection3*presvote
gen DaysToElec3Post1994=DaysToElection3*post1994elec
gen DaysToElec3PresPost1994=DaysToElection3*presvote*post1994elec

logit PartyUnityVote DaysToElection presvote post1994elec DaysToElecPres DaysToElecPost1994 DaysToElection2 DaysToElec2Pres DaysToElection3 DaysToElec3Pres QualChall QualChall_lag Freshman InpartyMidterm PresApproval_In rdi_q3_In PartyOfPres PrimaryChall redist seniorit Retirement_Jacob AbsNominate VoteShare_lag RegPass Susp MiscPro Amend ProPart if PresPartyUnity==1, vce(cluster distgroup)

drawnorm M1_b1-M1_b28, n(10000) means(e(b)) cov(e(V)) clear

*Enter values of variables

*EARLY PERIOD (FOR ALL VARIABLES)
forvalues i=1(1)735 {
scalar days=`i'
scalar days2=days^2
scalar days3=days^3
scalar presvote=53.80947
scalar post1994=0
scalar dayspres=days*presvote
scalar dayspost1994=days*post1994
scalar prespost1994=presvote*post1994
scalar daysprespost1994=days*presvote*post1994
scalar days2pres=days2*presvote
scalar days3pres=days3*presvote
scalar QualChall=.2365175
scalar QualChall_lag=.2285399
scalar Freshman=.2662885
scalar InpartyMidterm=.285537 
scalar PresApproval_In=-.803758
scalar rdi_q3_In=.1404514
scalar PartyOfPres=.4888875
scalar PrimaryChall=.3183035
scalar redist=.3576258 
scalar seniorit=6.469113
scalar Retirement_Jacob=.0899082
scalar AbsNominate=.2919597
scalar VoteShare_lag=66.27492
scalar RegPass=.4328308
scalar Susp=.0742217
scalar MiscPro=.0612949
scalar Amend=.187322 
scalar ProPart=.1963771  
scalar constant=1
generate x_betahatA_`i'=M1_b1*days + M1_b2*presvote + M1_b3*post1994 + M1_b4*dayspres + M1_b5*dayspost1994 + M1_b6*days2 + M1_b7*days2pres + M1_b8*days3 + M1_b9*days3pres + M1_b10*QualChall + M1_b11*QualChall_lag + M1_b12*Freshman + M1_b13*InpartyMidterm + M1_b14*PresApproval_In + M1_b15*rdi_q3_In + M1_b16*PartyOfPres + M1_b17*PrimaryChall + M1_b18*redist + M1_b19*seniorit + M1_b20*Retirement_Jacob + M1_b21*AbsNominate + M1_b22*VoteShare_lag + M1_b23*RegPass + M1_b24*Susp + M1_b25*MiscPro + M1_b26*Amend + M1_b27*ProPart + M1_b28*constant
generate predA_`i'=1/(1+exp(-(x_betahatA_`i')))
drop x_betahatA_`i'
}

*EARLY FOR CYCLE & VOTES (BUT LATE FOR PRESVOTE - TO REPRESENT CHANGE IN VULNERABILITY)
forvalues i=1(1)735 {
scalar days=`i'
scalar days2=days^2
scalar days3=days^3
scalar presvote=63.64906
scalar post1994=0
scalar dayspres=days*presvote
scalar dayspost1994=days*post1994
scalar prespost1994=presvote*post1994
scalar daysprespost1994=days*presvote*post1994
scalar days2pres=days2*presvote
scalar days3pres=days3*presvote
scalar QualChall=.2365175
scalar QualChall_lag=.2285399
scalar Freshman=.2662885
scalar InpartyMidterm=.285537 
scalar PresApproval_In=-.803758
scalar rdi_q3_In=.1404514
scalar PartyOfPres=.4888875
scalar PrimaryChall=.3183035
scalar redist=.3576258 
scalar seniorit=6.469113
scalar Retirement_Jacob=.0899082
scalar AbsNominate=.2919597
scalar VoteShare_lag=66.27492
scalar RegPass=.4328308
scalar Susp=.0742217
scalar MiscPro=.0612949
scalar Amend=.187322 
scalar ProPart=.1963771  
scalar constant=1
generate x_betahatB_`i'=M1_b1*days + M1_b2*presvote + M1_b3*post1994 + M1_b4*dayspres + M1_b5*dayspost1994 + M1_b6*days2 + M1_b7*days2pres + M1_b8*days3 + M1_b9*days3pres + M1_b10*QualChall + M1_b11*QualChall_lag + M1_b12*Freshman + M1_b13*InpartyMidterm + M1_b14*PresApproval_In + M1_b15*rdi_q3_In + M1_b16*PartyOfPres + M1_b17*PrimaryChall + M1_b18*redist + M1_b19*seniorit + M1_b20*Retirement_Jacob + M1_b21*AbsNominate + M1_b22*VoteShare_lag + M1_b23*RegPass + M1_b24*Susp + M1_b25*MiscPro + M1_b26*Amend + M1_b27*ProPart + M1_b28*constant
generate predB_`i'=1/(1+exp(-(x_betahatB_`i')))
drop x_betahatB_`i'
}

*LATE FOR ALL
forvalues i=1(1)735 {
scalar days=`i'
scalar days2=days^2
scalar days3=days^3
scalar presvote=63.64906
scalar post1994=1
scalar dayspres=days*presvote
scalar dayspost1994=days*post1994
scalar prespost1994=presvote*post1994
scalar daysprespost1994=days*presvote*post1994
scalar days2pres=days2*presvote
scalar days3pres=days3*presvote
scalar QualChall=.098179
scalar QualChall_lag=.1419985
scalar Freshman=.2537902
scalar InpartyMidterm=.5281861
scalar PresApproval_In=-.994198
scalar rdi_q3_In=.3418647 
scalar PartyOfPres=.5460493
scalar PrimaryChall=.3716194
scalar redist=.0425769 
scalar seniorit=5.573624
scalar Retirement_Jacob=.1415856
scalar AbsNominate=.4427426
scalar VoteShare_lag=63.71035
scalar RegPass=.0564774
scalar Susp=.05562
scalar MiscPro=.7091023
scalar Amend=.0633329 
scalar ProPart=.1068932 
scalar constant=1
generate x_betahatC_`i'=M1_b1*days + M1_b2*presvote + M1_b3*post1994 + M1_b4*dayspres + M1_b5*dayspost1994 + M1_b6*days2 + M1_b7*days2pres + M1_b8*days3 + M1_b9*days3pres + M1_b10*QualChall + M1_b11*QualChall_lag + M1_b12*Freshman + M1_b13*InpartyMidterm + M1_b14*PresApproval_In + M1_b15*rdi_q3_In + M1_b16*PartyOfPres + M1_b17*PrimaryChall + M1_b18*redist + M1_b19*seniorit + M1_b20*Retirement_Jacob + M1_b21*AbsNominate + M1_b22*VoteShare_lag + M1_b23*RegPass + M1_b24*Susp + M1_b25*MiscPro + M1_b26*Amend + M1_b27*ProPart + M1_b28*constant
generate predC_`i'=1/(1+exp(-(x_betahatC_`i')))
drop x_betahatC_`i'
}

foreach var of varlist predA_1-predA_735{
egen `var'a=clsort(`var')
}

drop predA_1-predA_735

foreach var of varlist predB_1-predB_735{
egen `var'b=clsort(`var')
}

drop predB_1-predB_735

foreach var of varlist predC_1-predC_735{
egen `var'c=clsort(`var')
}

drop predC_1-predC_735

save "Figure5b_Values.dta"

drop M1_b1-M1_b28
gen days=_n
*Keep the 8.25th and 91.75th percentiles
keep if days==825 | days==9175
xpose, clear
rename v1 LowBound
rename v2 UpBound
drop in 2206
gen days=_n
move days LowBound
gen Type="A - All Early" if days<736
replace Type="B - Late Presvote" if days>735 & days<1471
replace Type="C - All Late" if days>1470 & days<2206
replace days=days-735 if Type=="B - Late Presvote"
replace days=days-1470 if Type=="C - All Late"

*sort days

save "Figure5b_Preds.dta"

clear
*Reshape for figure
use "Figure5b_Preds.dta"
gen shaper=1 if Type=="A - All Early"
replace shaper=2 if Type=="B - Late Presvote"
replace shaper=3 if Type=="C - All Late"
drop Type
reshape wide LowBound UpBound, i(days) j(shaper)
save "Figure5b_Preds_reshaped.dta"
clear
